﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.BUS.Workers;
using ECare.Client.templates;

namespace ECare.Client.frms
{
    public partial class frmOutInfo : frmInfo
    {
        public frmOutInfo()
        {
            InitializeComponent();

            this.Load += frmOutInfo_Load;

            this.grdPlans.CellFormatting += grdPlans_CellFormatting;

            

        }

        void grdPlans_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.RowIndex < grdPlans.Rows.Count)
            {
                if (grdPlans.Columns[e.ColumnIndex].Name == "StorageNumber")
                {
                    decimal NeededNumber = (decimal)this.grdPlans.Rows[e.RowIndex].Cells["NeededNumber"].Value;
                    decimal ProduceNumber = (decimal)this.grdPlans.Rows[e.RowIndex].Cells["ProduceNumber"].Value;
                    decimal StorageNumber = (decimal)this.grdPlans.Rows[e.RowIndex].Cells["StorageNumber"].Value;

                    if (NeededNumber - ProduceNumber > StorageNumber)
                        this.grdPlans.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                    else if (StorageNumber == 0)
                        this.grdPlans.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                    else
                    {

                    }
                }
            }
        }


        public List<PlanInfo> Plans { get; set; }

        void frmOutInfo_Load(object sender, EventArgs e)
        {

            this.txtCode.Text = DateTime.Now.ToString("yyyyMMddhhmmss");


            var loader = new CreateResourcesWorker();
            loader.Plans = this.Plans;
            loader.RunWorkerCompleted += loader_RunWorkerCompleted;
            loader.RunWorkerAsync();
            frmloading = new frmLoading();
            frmloading.ShowDialog();
        }

        void loader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            frmloading.Close();



            resourceId.DataSource = ResourcesHelper.GetAllResources();
            resourceId.DisplayMember = "ResourceName";
            resourceId.ValueMember = "ResourceId";


            grdPlans.DataSource = e.Result as DataTable;
            //    this.grdPlans.Rows[0].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //   if(this.)

            if (!ValidateTextControl(this.txtCode))
                return;

            for (int i = 0; i < this.grdPlans.Rows.Count; i++)
            {
                if (this.grdPlans.Rows[i].DefaultCellStyle.ForeColor == Color.Red)
                {
                    MessageBox.Show("库存不足，不能生成领料单.", "信息提示", MessageBoxButtons.OK);
                    this.tabControl1.SelectedIndex = 1;
                    return;
                }
            }


            var planoutinfo = new PlanOutInfo();
            planoutinfo.plan_out_code = this.txtCode.Text;
            planoutinfo.plan_remark = this.txtRemark.Text;
            planoutinfo.plan_out_date = this.txtOutDate.Value;
            planoutinfo.plan_produce_date = this.txtProduceDate.Value;

            var resource = new Dictionary<int, decimal>();
            for (int i = 0; i < this.grdPlans.Rows.Count; i++)
            {
                int resourceId = (int)this.grdPlans.Rows[i].Cells["resourceId"].Value;
                decimal producnumber=(decimal)this.grdPlans.Rows[i].Cells["ProduceNumber"].Value;
                decimal NeededNumber = (decimal)this.grdPlans.Rows[i].Cells["NeededNumber"].Value;

                if(producnumber<NeededNumber)
                {
                     resource.Add(resourceId, NeededNumber-producnumber);
                }
            }
            //

            var createresult=PlanHelper.CreatePlanOut(planoutinfo,Plans,resource);

            if (createresult > 0)
            {
                planoutinfo.plan_out_id = createresult;
                foreach (var plan in Plans)
                {
                    plan.plan_IsGenerateResources = true; 
                   
                }

                this.Close();
            }
        }
    }
}
